from django.shortcuts import render
from django.shortcuts import HttpResponse
import json,time
from db.mydb import MyDBConnection
from db.dao.send_logs import Send_logs
from db.utils.dfutils import load_json_to_object
from db.utils.logutils import LogUtils
from db.dao.students import Students
def send_logs_index(request):
    current = 1#如果用户不提供，默认看第一页
    pagesize = 10#如果用户不提供，默认每页5条
    if request.method == 'GET':  #大小写
        myDBConnection = MyDBConnection()
        current = request.GET.get("current")
        pagesize = request.GET.get("pagesize")
        print(f'1111 {request.GET.get("current")}')
        offset = (int(current) - 1) * int(pagesize) #计算每页的第一条编号
        send_logss_json = myDBConnection.fetch_data_by_sql(f"select * from send_logs limit {offset},{pagesize}")
        msg = f'现在查看的是第{current}页，每页大小{pagesize}'
        print(send_logss_json)
        code = 200
        rep_json =  {"code":code,"msg":msg,"send_logss":send_logss_json}
    if bool(request.GET.get('json'))==True:
        return HttpResponse(json.dumps(rep_json), content_type="application/json")
    else:
        return render(request, "send_logs/index.html", rep_json)


def send_logsById(request):
    current = 1#如果用户不提供，默认看第一页
    pagesize = 5#如果用户不提供，默认每页5条
    if request.method == 'GET':  #大小写
        myDBConnection = MyDBConnection()
        current = request.GET.get("current")
        pagesize = request.GET.get("pagesize")
        j_id = request.GET.get("j_id")

        offset = (int(current) - 1) * int(pagesize) #计算每页的第一条编号
        send_logs_json = myDBConnection.fetch_data_by_sql(f"select * from send_logs where j_id={j_id} limit {offset},{pagesize} ")
        session = myDBConnection.get_session()
        send_logss=[]
        for send_logs in send_logs_json:
            student = session.query(Students).filter(Students.st_id == send_logs["s_id"]).first()
            print("----------------")
            print(type(student))
            send_logs["_student"]=student.to_json()
            send_logss.append(send_logs)
        session.flush()
        session.close()
        msg = f'现在查看的是第{current}页，每页大小{pagesize}'
        print(send_logss)
        code = 200
        rep_json =  {"code":code,"msg":msg,"send_logs":send_logss}
    if bool(request.GET.get('json'))==True:
        return HttpResponse(json.dumps(rep_json,ensure_ascii=False), content_type="application/json")
    else:
        return render(request, "enrollment/index.html", rep_json)


def send_logs_add(request):
    # p_id = request.POST.get("p_id")
    # p_name = request.POST.get("p_name")
    data = request.body.decode("utf-8")
    print(type(data))
    myDBConnection =MyDBConnection()
    send_logs_json_obj = json.loads(data)
    print(send_logs_json_obj)
    send_logs = load_json_to_object(send_logs_json_obj,Send_logs())
    print(type(send_logs))
    myDBConnection.insert_entity(send_logs)
    pname = send_logs.se_id
    print(pname)

    # path_name = 'd:log'
    path_name = '/usr/ftp/logs/send_logs/'
    current_time = time.localtime(time.time())

    current_time_str = time.strftime('%Y-%m-%d', current_time)  # 参数2为格式化字符串
    print(current_time_str)
    file_name = current_time_str + 'send_logs.log'
    LogUtils.log(path_name, file_name, send_logs.format_str())
    # LogUtils.log(path_name,file_name)
    
    msg = f"{pname}球员添加成功"
    code = 200
    rep_json = {"code":code,"msg":msg}
    return HttpResponse(json.dumps(rep_json), content_type="application/json")